#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2019/7/18 16:09
# @Author   : BYF
# file      : assert.py
# @version  :

from common.request import Request

class Assert():

    def __init__(self,arg1,arg2,test_msg):
        self.arg1=arg1
        self.arg2=arg2
        self.test_msg=test_msg


    def assertEqual(self):
        if self.arg1==self.arg2:
            test_result='pass'
            error_msg=''
        else:
            test_result='ERROR'
            error_msg=self.test_msg

        return test_result,error_msg

    def assertNotEqual(self):
        if self.arg1!=self.arg2:
            test_result='pass'
            error_msg=''
        else:
            test_result='ERROR'
            error_msg=self.test_msg

        return test_result,error_msg

    def assertIn(self):
        arg1 = str(self.arg1)
        arg2 = str(self.arg2)
        """问题：列表、字典判断包含时，有问题"""
        if arg1 in arg2:
            test_result='pass'
            error_msg=''
        else:
            test_result='ERROR'
            error_msg=self.test_msg
        return test_result,error_msg


class AssertResponse():

    def __init__(self,res):
        #定义接口请求的返回值
        self.res=res

    def assertResult(self,test_status_code,test_status,test_msg):
        res=self.res

        error_msg=res['response'] #获取接口的内容作为错误的信息 msg
        status_code=res['status_code'] #获取接口请求状态码status_code

        # 获取接口返回状态码（java）code 、 （PHP）status
        response=res['response']
        if type(response)==type({}):
            if 'code' in response.keys():
                status=response['code']
            elif 'status' in response.keys():
                status=response['status']
            else:
                status =None
        else:
            status=None

        # 获取接口返回信息（java）msg
        response=res['response']
        if type(response)==type({}):
            if 'msg' in response.keys():
                msg=response['msg']
            elif 'message' in response.keys():
                msg=response['message']
            else:
                msg =None
        else:
            msg=None

        error_msg=error_msg
        if status_code!=None:
            res_status_code=int(status_code)
        if status!=None:
            res_status=int(status)
        if msg!=None:
            res_msg=str(msg)


        # 断言接口请求状态码：status_code
        test_result1, error_msg1 = Assert(test_status_code,res_status_code, error_msg).assertEqual()
        # 断言接口返回值状态码：(php)status/(java)code
        test_result2, error_msg2 = Assert(test_status,res_status, error_msg).assertEqual()
        # 断言接口返回信息：msg
        test_result3, error_msg3 = Assert(test_msg, res_msg, error_msg).assertEqual()

        if test_result1=='pass':
            if test_result2 == 'pass':
                if test_result3 == 'pass':
                    test_result='pass'
                    error_msg=''
                else:
                    test_result='ERROR'
                    error_msg=error_msg3
            else:
                test_result='ERROR'
                error_msg=error_msg2
        else:
            test_result='ERROR'
            error_msg = error_msg1

        return test_result,error_msg


